package org.apak.frontend;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apak.control.DeviceForm;
import org.apak.db.RepairServiceDAO;
import org.apak.log.FileLogger;
import org.apak.model.Device;
import org.apak.model.DeviceType;
import org.apak.model.LoggedSubject;
import org.apak.model.ServiceDevice;
import org.apak.model.ServiceRequest;

/**
 * Servlet implementation class DeviceServlet
 */
public class DeviceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RepairServiceDAO dao = new RepairServiceDAO();
	private String serviceOrder;
	private HttpSession session;
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
		LoggedSubject loggedSubject = (LoggedSubject) session.getAttribute("loggedSubject");
		if(loggedSubject == null || loggedSubject.isCustomer()) {
			RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
			dispatcher.forward(request, response);
		} else {
			Device[] deviceArray = dao.getDeviceList();
			request.setAttribute("deviceArray", deviceArray);
			DeviceType[] deviceTypeArray = dao.getDeviceTypeList();
			request.setAttribute("deviceTypeArray", deviceTypeArray);
			serviceOrder = request.getParameter("serviceOrder");

			if(dao.getServiceOrder(serviceOrder) != null) {
				if(serviceOrder != null) {
					ServiceDevice[] serviceDeviceArray = dao.getServiceDeviceList(serviceOrder);
					request.setAttribute("serviceDeviceArray", serviceDeviceArray);
				}
			} else {
				RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
				dispatcher.forward(request, response);
				return;
			}
			FileLogger.logMessage("Forwarded to device.jsp by DeviceServlet.doGet().");
			RequestDispatcher dispatcher = request.getRequestDispatcher("device.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String actionButton = request.getParameter("actionButton");
		if(actionButton != null)
			if(actionButton.equals("New device")) {
				FileLogger.logMessage("Forwarded to /deviceInsert by DeviceServlet.doPost().");
				response.sendRedirect("./deviceInsert");
			} else if(actionButton.equals("Search")) {
				DeviceType[] deviceTypeArray = dao.getDeviceTypeList();
				DeviceForm deviceForm = new DeviceForm(request.getParameter("name"),
						request.getParameter("reg_no"), request.getParameter("description"),
						request.getParameter("model"), request.getParameter("manufacturer"), request.getParameter("device_type"), request.getParameter("customerName"));
				Device[] deviceArray = dao.searchDevice(deviceForm);
				request.setAttribute("deviceTypeArray", deviceTypeArray);
				request.setAttribute("deviceForm", deviceForm);
				request.setAttribute("deviceArray", deviceArray);
				if(serviceOrder != null) {
					ServiceDevice[] serviceDeviceArray = dao.getServiceDeviceList(serviceOrder);
					request.setAttribute("serviceDeviceArray", serviceDeviceArray);
				}
				FileLogger.logMessage("Forwarded to device.jsp by DeviceServlet.doPost().");
				RequestDispatcher dispatcher = request.getRequestDispatcher("device.jsp");
				dispatcher.forward(request, response);
			}
	}

}
